package day01am;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author JokerQ
 * @date 2020/8/22 - 23:42
 */
public class URLDemo {
    public static void main(String[] args) throws IOException {
        //如果url是图片，则下载该图片
        String imgUrl =
                "https://new.qq.com/rain/a/20200823A04RUN00";
        //创建URL对象，并通过调用openConnection方法获得URLConnection对象
        URL url = new URL("http://www.tmooc.cn/");
        URLConnection connection = url.openConnection();
        System.out.println("Content-Type: " + connection.getContentType());
        System.out.println("Content-Length: " + connection.getContentLength());
        System.out.println("Content-LengthLong: " + connection.getContentLengthLong());
        System.out.println("Date: " + connection.getDate()/(1000*60*60*24));
        if (connection!=null){
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            FileOutputStream out = new FileOutputStream("1.html");
            BufferedOutputStream bos = new BufferedOutputStream(out);
            int c ;
            while ((c=bis.read())!=-1){
                //System.out.println((char)c);
                bos.write(c);
                bos.flush();
            }
            System.out.println("输出成功！！！");
            is.close();
        }
    }
}
